package base.interactive.http;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * Rest template config
 *
 * @description: RestTemplate配置
 * @author: cyj
 * @date 2020 -10-27 14:45:14
 */
@Configuration
public class RestTemplateConfig {

    /**
     * Rest template rest template
     *
     * @param factory factory
     * @return the rest template
     * @description: http请求
     */
    @Bean("httpRestTemplate")
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    /**
     * Rest template rest template
     *
     * @return the rest template
     * @description: https请求忽略证书
     */
    @Bean("httpsRestTemplate")
    public RestTemplate restTemplate() {
        return new RestTemplate(generateHttpRequestFactory());
    }

    /**
     * Simple client http request factory client http request factory
     *
     * @return the client http request factory
     * @description:
     */
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setConnectTimeout(1000 * 15);
        factory.setReadTimeout(1000 * 15);
        return factory;
    }

    /**
     * Generate http request factory http components client http request factory
     *
     * @return the http components client http request factory
     * @description:
     */
    public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory() {
        TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
        SSLContext sslContext = null;
        try {
            sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        } catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
            e.printStackTrace();
        }
        SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
        HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(httpClient);
        factory.setConnectTimeout(1000 * 15);
        factory.setReadTimeout(1000 * 15);
        return factory;
    }
}
